#ifndef WIFIREFLASH_H
#define WIFIREFLASH_H

#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include "../weatherNum/weatherNum.h"
#include "config.h"
#include "NTPClient.h"
#include "TimeLib.h"

#define NTP_PACKET_SIZE 48 // NTP时间在消息的前48字节中

struct WeatherInfoStructure
{
    int8_t temperature; // 温度
    uint8_t humidity;    // 湿度
    String cityName;     // 城市
    uint16_t pm25Value;     // pm2.5
    weatherTpye weatherCode;   // 天气代码
    String weather;        // 实时天气
    String windDirection;  // 风向
    String windSpeed;      // 风速
    String todayWeather;   // 今日天气
    weatherTpye todayWeatherCode;   // 今日天气代码
    int8_t maxTemperature; // 最高温度
    int8_t minTemperature; // 最低温度
};

class WifiReFlash
{
private:
    const String _ntpServerPool[8] = {{"ntp.aliyun.com"},      // 备用NTP服务器域名
                                        {"ntp1.aliyun.com"},
                                        {"ntp2.aliyun.com"},
                                        {"ntp3.aliyun.com"},
                                        {"ntp4.aliyun.com"},
                                        {"ntp5.aliyun.com"},
                                        {"ntp6.aliyun.com"},
                                        {"s2g.time.edu.cn"}};
    // IPAddress _ntpServerIP;             // NTP服务器IP
    uint8_t _ntpServerIndex;
    const uint8_t _timeZone = 8;         //东八区
    byte _packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming & outgoing packets
    HTTPClient _httpClient;
    WiFiUDP _udp;
    WiFiClient _wificlient;

    uint32_t _localPort = 123;          // UDP端口号
    void _sendNTPpacket();

public:
    time_t ntpTime;

    WifiReFlash();
    bool getNtpTime();
    bool getNtpTime(uint32_t localPort);
    String getCityCode();
    uint8_t getCityWeather(String cityCode, WeatherInfoStructure* weatherInfo);
};

#endif
